namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdviceEditPage : Page
    {
        protected TextBox AdviceContentTxb;
        protected TextBox AdviceReContentTxb;
        protected TextBox AdviceTitleTxb;
        protected DropDownList areaDdl;
        protected Image iAdvice;
        protected Label lbAdviceContent;
        protected Label lbAdviceReContent;
        protected Label lbAdviceTime;
        protected Label lbAdviceTimeValue;
        protected Label lbAdviceTitle;
        protected Label lbArea;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;

        private void InitializeComponent()
        {
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            this.SetFromImage();
            this.areaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.areaDdl.Items.Add(tmpListItem);
            }
            if (this.Principal.SysUser.IsAdmin > 0)
            {
                this.AdviceReContentTxb.ReadOnly = false;
                this.AdviceTitleTxb.ReadOnly = true;
                this.AdviceContentTxb.ReadOnly = true;
                this.areaDdl.Enabled = false;
            }
            else
            {
                this.AdviceReContentTxb.ReadOnly = true;
                this.AdviceTitleTxb.ReadOnly = false;
                this.AdviceContentTxb.ReadOnly = false;
            }
            if (base.Request.QueryString["IsNew"] == "True")
            {
                this.AdviceReContentTxb.Visible = false;
                this.lbAdviceReContent.Visible = false;
                this.lbAdviceTimeValue.Visible = false;
                this.lbAdviceTime.Visible = false;
            }
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["AdviceId"]);
                Advice advice = ManageAdvice.GetAdviceInfoViaId(id);
                this.AdviceTitleTxb.Text = advice.AdviceTitle;
                this.AdviceContentTxb.Text = advice.AdviceContent;
                this.AdviceReContentTxb.Text = advice.AdviceReContent;
                this.lbAdviceTimeValue.Text = advice.AdviceTime.ToString();
                if ((this.Principal.SysUser.IsAdmin <= 0) && (advice.AdviceReContent == ""))
                {
                    this.AdviceReContentTxb.Visible = false;
                    this.lbAdviceReContent.Visible = false;
                }
                for (int i = 0; i < this.areaDdl.Items.Count; i++)
                {
                    if (this.areaDdl.Items[i].Value == advice.AreaId.ToString())
                    {
                        this.areaDdl.SelectedIndex = i;
                    }
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strAdviceTitleTxb = this.AdviceTitleTxb.Text;
            string strAdviceContentTxb = this.AdviceContentTxb.Text;
            string strAdviceReContentTxb = this.AdviceReContentTxb.Text;
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strAdviceTitleTxb != "")
                {
                    ManageAdvice.UpdateAdviceInfo(new Guid(base.Request.QueryString["AdviceId"]), strAdviceTitleTxb, strAdviceContentTxb, strAdviceReContentTxb, new Guid(this.areaDdl.SelectedValue));
                    base.Response.Redirect("AdvicePage.aspx", true);
                }
                base.Response.Write("<script> alert('Title is Null !') </script>");
            }
            else if (strAdviceTitleTxb != "")
            {
                ManageAdvice.InsertNewAdviceInfo(DateTime.Now, strAdviceTitleTxb, strAdviceContentTxb, strAdviceReContentTxb, new Guid(this.areaDdl.SelectedValue), this.Principal.SysUser.UserId);
                base.Response.Redirect("AdvicePage.aspx", true);
            }
        }

        private void SelectAuth()
        {
            this.InitPage();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbArea.Text = ManageSystemConfig.GetSystemSetLanguage("AdviceEditPage", "lbArea", language, path);
            this.lbAdviceTitle.Text = ManageSystemConfig.GetSystemSetLanguage("AdviceEditPage", "lbAdviceTitle", language, path);
            this.lbAdviceContent.Text = ManageSystemConfig.GetSystemSetLanguage("AdviceEditPage", "lbAdviceContent", language, path);
            this.lbAdviceReContent.Text = ManageSystemConfig.GetSystemSetLanguage("AdviceEditPage", "lbAdviceReContent", language, path);
            this.lbAdviceTime.Text = ManageSystemConfig.GetSystemSetLanguage("AdviceEditPage", "lbAdviceTime", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("AdviceEditPage", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iAdvice.ImageUrl = "images/title_advice.jpg";
                    break;

                case "en":
                    this.iAdvice.ImageUrl = "images/title_adviceEn.jpg";
                    break;
            }
        }
    }
}

